
function TabControl( prop ) {
    if( typeof prop.tabID != 'string' || prop.tabID == '' )
        return;
        
    this.tabID = prop.tabID;
    this.title = (typeof prop.title == 'string' ) ? prop.title : '';    
    this.show = (typeof prop.show == 'function' ) ? prop.show : function() {};
    this.hide = (typeof prop.hide == 'function' ) ? prop.hide : function() {};
    this.data = (typeof prop.data != 'undefined' ) ? prop.data : new Array();
    
    if( typeof prop.init == 'function' )
        prop.init( this.tabID );    
}

function TabSwitcher( tabsID ) {
    var tabs = new Array();
    var ts = this;
    var t = $('#'+tabsID).tabs( {
        select: function(ev,ui) {
            ts.onSwitchTo(ui.panel.id);
        }
    } );

    function index( tabID ) {
        var result;
        for( var i=0; i<tabs.length; i++) {
            if( tabs[i].tabID == tabID ) {
                result = i; break;
            }
        }
        return result;
    }
    
    this.find = function( tabID ) {
        var i = ( typeof(tabID) == 'string' && tabID != '') ? index( tabID ) : tabID;
        return tabs[i];
    }
    
    this.add = function( tab ) {
        if( typeof tab.tabID != 'string' || tab.tabID == '' ) return;
        if( typeof index( tab.tabID ) != 'undefined' ) return;

        var a = new Object();
        
        a.tabID = tab.tabID;
        a.title = (typeof tab.title == 'string' ) ? tab.title : a.tabID;    
        a.show  = (typeof tab.show == 'function' ) ? tab.show : function() {};
        a.hide  = (typeof tab.hide == 'function' ) ? tab.hide : function() {};
        a.data  = (typeof tab.data != 'undefined' ) ? tab.data : new Array();
    
        a.init  = (typeof tab.init == 'function' ) ? tab.init : function() {};
        a.destroy = (typeof tab.destroy == 'function' ) ? tab.destroy : function() {};
        tabs.push( a );

//        $('body').append('<div id='+a.tabID+' class=".ui-tabs .ui-tabs-hide"></div>');
        t.tabs('add','#'+a.tabID,a.title);
                 
        a.init( a.tabID, tabs.length - 1 );    
    }
    
    this.remove = function( tabID ) {
        var i = ( typeof(tabID) == 'string' && tabID != '') ? index( tabID ) : tabID;
        var a =  tabs[i];
        
        a.destroy();
        tabs.splice(i,1);
        t.tabs('remove', i);
//        $('body > #'+a.tabID).remove();
        delete a;
    }
    
    this.onSwitchTo = function( tabID ) {
        var inew = ( typeof(tabID) == 'string' && tabID != '') ? index( tabID ) : tabID;
        var iold = t.tabs('option', 'selected');        
        tabs[iold].hide();
        tabs[inew].show();
    }
    
    this.SwitchTo = function( tabID ) {
        var i = ( typeof(tabID) == 'string' && tabID != '') ? index( tabID ) : tabID;
        t.tabs('select', i);        
    }
}
